package com.mycompany.videoclub;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import javax.swing.JOptionPane;

public class Conectar {

    public String user;
    public String pass;
    public String url;
    public Connection cn =null;
    
    public Connection conexion() throws IOException {
        
        Properties propiedadesbase = new Properties();
        String basedatos =("/configuration.properties");
        propiedadesbase.load(Conectar.class.getResourceAsStream(basedatos));
        String basededatos = propiedadesbase.getProperty("basededatos");
        
        if ("MySQL".equals(basededatos)){
            Properties propiedadesmysql = new Properties();
            String basemysql =("/configuracionMySQL.properties");
            propiedadesmysql.load(Conectar.class.getResourceAsStream(basemysql));

            url = propiedadesmysql.getProperty("url");
            user =  propiedadesmysql.getProperty("user");
            pass = propiedadesmysql.getProperty("pass");
            if( !url.isEmpty()){
                try{
                    Class.forName("com.mysql.jdbc.Driver");
                    cn = DriverManager.getConnection(url,user,pass);
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(null, "Error" + ex.getMessage());
                }   
            }
        }
        if ("HSQL".equals(basededatos)){
            Properties propiedadeshsql = new Properties();
            String basehsql =("/configuracionHSQL.properties");
            propiedadeshsql.load(Conectar.class.getResourceAsStream(basehsql));
            url = propiedadeshsql.getProperty("url");
            
            try{
                Class.forName("org.hsqldb.jdbcDriver");
                cn = DriverManager.getConnection(url);
            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "Error" + ex.getMessage());
            }
        }
        return cn;
    }
}
